//
//  serverListViewController.m
//  changAnCommercial
//
//  Created by 高文富 on 16/11/28.
//  Copyright © 2016年 高文富. All rights reserved.
//

#import "serverListViewController.h"
#import "addServerViewController.h"
#import "ViewController.h"

@interface serverListViewController ()<addServerDelegate,loginDelegate>

@end

@implementation serverListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationController.navigationBar.hidden = NO;
     self.title=@"服务器列表";
    currentIndex=0;
    [self addRightBarButton];
    
    _notificationArray=[[NSMutableArray alloc]init];
    NSArray *newList= [[NSUserDefaults standardUserDefaults]objectForKey:@"serverList"];
    [_notificationArray addObjectsFromArray:newList];
  
    
    
}

-(void)viewWillAppear:(BOOL)animated{

    self.navigationController.navigationBar.hidden = NO;
}

- (void)addRightBarButton
{
    //  添加切换城市点击事件
    UIButton *rightButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 25, 25)];
    [rightButton addTarget:self action:@selector(addSever) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setBackgroundColor:[UIColor clearColor]];
    [rightButton setImage:[UIImage imageNamed:@"addOtherCar"] forState:UIControlStateNormal];
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}


-(void)addSever{
    addServerViewController *addServer=[[addServerViewController alloc]init];
    addServer.delegate=self;
    addServer.title=@"添加服务器";
    [self.navigationController pushViewController:addServer animated:YES];;


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 添加服务器代理
-(void)updateServerData:(NSString *)nickName address:(NSString *)address{
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:nickName forKey:@"serverName"];
    [dic setObject:address forKey:@"serverAddress"];
    [_notificationArray addObject:dic];
    [self storageData:_notificationArray];

}

#pragma mark - 修改数据

-(void)updateValue:(NSString *)key Value:(NSString *)Value{

    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithDictionary:[_notificationArray objectAtIndex:currentIndex]];
    [dic setObject:Value forKey:key];
    [_notificationArray replaceObjectAtIndex:currentIndex withObject:dic];
    [self storageData:_notificationArray];
}

-(void)updateValue:(NSString *)serverName serverAddress:(NSString *)serverAddress{

    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithDictionary:[_notificationArray objectAtIndex:currentIndex]];
    [dic setObject:serverName forKey:@"serverName"];
    [dic setObject:serverAddress forKey:@"serverAddress"];
    [_notificationArray replaceObjectAtIndex:currentIndex withObject:dic];
    [self storageData:_notificationArray];

}


-(void)exitClearValue{
    
  
    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithDictionary:[_notificationArray objectAtIndex:currentIndex]];
    for (NSString *key in [dic allKeys]) {
        if ([key isEqualToString:@"SP_AUTO"]) {
              [dic setObject:@"0" forKey:key];
        }else if ([key isEqualToString:@"SP_USERNAME"]){
        
              [dic setObject:@"" forKey:key];
        }
        else if ([key isEqualToString:@"SP_PASSWORD"]){
            
              [dic setObject:@"" forKey:key];
        }
    }
     [_notificationArray replaceObjectAtIndex:currentIndex withObject:dic];
     [self storageData:_notificationArray];
}


#pragma mark - TableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.notificationArray count];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text=[[_notificationArray objectAtIndex:indexPath.row] objectForKey:@"serverName"] ;
    return cell;
}

#pragma mark - TableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    currentIndex=(int)indexPath.row;
    ViewController *homeView=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    homeView.dicValue=[_notificationArray objectAtIndex:indexPath.row];
    homeView.delegate=self;
    [self.navigationController pushViewController:homeView animated:YES];

  
}


-(NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     currentIndex=(int)indexPath.row;
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
                                                                         title:@"删除"
                                                                       handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                                                                           
                                                                           [_notificationArray removeObjectAtIndex:indexPath.row];
                                                                            [self storageData:_notificationArray];

                                                                       }];
    UITableViewRowAction *rowActionSec = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault
                                                                            title:@"修改"
                                                                          handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                                                                              
                                addServerViewController *addServer=[[addServerViewController alloc]init];
                                addServer.delegate=self;
                                addServer.dic=[_notificationArray objectAtIndex:indexPath.row];
                                addServer.title=@"修改服务器";
                                [self.navigationController pushViewController:addServer animated:YES];
                                                                              
                                                                          }];
    rowActionSec.backgroundColor = [UIColor blueColor];
    NSArray *arr = @[rowAction,rowActionSec];
    return arr;
}






//修改数据。
-(void)storageData:(NSMutableArray *)list{

    [[NSUserDefaults standardUserDefaults]setObject:list forKey:@"serverList"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [_serverlistView reloadData];
}


@end
